package com.gwnet.rider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwnet.common.enums.AmountTypeEnum;
import com.gwnet.common.enums.IOTypeEnum;
import com.gwnet.common.enums.OwnerTypeEnum;
import com.gwnet.common.enums.ShopWalletChangeReason;
import com.gwnet.common.util.Arith;
import com.gwnet.rider.domain.PlatformWalletLog;
import com.gwnet.rider.domain.RiderWallet;
import com.gwnet.rider.mapper.RiderWalletMapper;
import com.gwnet.rider.service.PlatformWalletLogService;
import com.gwnet.rider.service.RiderWalletService;
import com.gwnet.shop.domain.OrderInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 骑手钱包表 服务实现类
 * </p>
 *
 * @author WangLiang
 * @since 2024-08-06
 */
@Service
@AllArgsConstructor
public class RiderWalletServiceImpl extends ServiceImpl<RiderWalletMapper, RiderWallet> implements RiderWalletService {

    private final RiderWalletMapper riderWalletMapper;
    private final PlatformWalletLogService platformWalletLogService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addAmountBySettledOrder(OrderInfo orderInfo) {
        // 查询该订单是否有已结算记录
        Long count = platformWalletLogService.getSettledCount(orderInfo.getOrderId(), OwnerTypeEnum.RIDER.code());
        // 幂等
        if (count > 0) {
            return;
        }

        // 查询待结算记录，生成已结算记录
        List<PlatformWalletLog> list = platformWalletLogService.getLogByOrderId(orderInfo.getOrderId(), OwnerTypeEnum.RIDER.code());

        for (PlatformWalletLog log : list) {
            if(AmountTypeEnum.UNSETTLED_AMOUNT.code().equals(log.getAmountType())){

                // 骑手钱包信息
                RiderWallet riderWallet = riderWalletMapper.selectById(log.getOwnerId());
                if(riderWallet.getUnsettledAmount() == 0){
                    continue;
                }

                // 需要计算的金额 骑手未结算金额
                double riderUnsettledAmount = 0.0;
                // 骑手已计算金额
                double riderSettledAmount = 0.0;

                // 扣除未结算金额
                PlatformWalletLog minusLog = new PlatformWalletLog();
                minusLog.setOwnerId(log.getOwnerId());
                minusLog.setOwnerType(OwnerTypeEnum.RIDER.code());
                minusLog.setOrderId(orderInfo.getOrderId());
                minusLog.setDevId(log.getDevId());
                // 扣款就是支出
                minusLog.setIoType(IOTypeEnum.OUT.code());
                minusLog.setAmountType(AmountTypeEnum.UNSETTLED_AMOUNT.code());
                minusLog.setChangeAmount(log.getChangeAmount());
                minusLog.setReason(ShopWalletChangeReason.SETTLED.value());
                minusLog.setUserAmount(log.getUserAmount());
                minusLog.setShopReduceAmount(log.getShopReduceAmount());
                minusLog.setPlatformAmount(log.getPlatformAmount());
                minusLog.setPlatformCommission(log.getPlatformCommission());
                minusLog.setTotalAmount(log.getTotalAmount());
                platformWalletLogService.save(minusLog);


                // 骑手钱包的未结算金额减去该记录
                riderUnsettledAmount = Arith.sub(riderWallet.getUnsettledAmount(),log.getChangeAmount());
                riderWallet.setUnsettledAmount(riderUnsettledAmount);



                // 增加可提现金额
                PlatformWalletLog addLog = new PlatformWalletLog();
                addLog.setOwnerId(log.getOwnerId());
                addLog.setOwnerType(OwnerTypeEnum.RIDER.code());
                addLog.setOrderId(orderInfo.getOrderId());
                addLog.setDevId(log.getDevId());
                addLog.setIoType(IOTypeEnum.IN.code());
                addLog.setAmountType(AmountTypeEnum.SETTLED_AMOUNT.code());
                addLog.setChangeAmount(log.getChangeAmount());
                addLog.setReason(ShopWalletChangeReason.SETTLED.value());
                addLog.setUserAmount(log.getUserAmount());
                addLog.setShopReduceAmount(log.getShopReduceAmount());
                addLog.setPlatformAmount(log.getPlatformAmount());
                addLog.setPlatformCommission(log.getPlatformCommission());
                addLog.setTotalAmount(log.getTotalAmount());
                platformWalletLogService.save(addLog);

                // 骑手钱包的已结算金额加上该记录
                riderSettledAmount = Arith.add(riderWallet.getSettledAmount(),log.getChangeAmount());
                riderWallet.setSettledAmount(riderSettledAmount);

                // 骑手钱包累计结算金额加上该记录
                double riderTotalSettledAmount = Arith.add(riderWallet.getTotalSettledAmount(), log.getChangeAmount());
                riderWallet.setTotalSettledAmount(riderTotalSettledAmount);

                riderWalletMapper.updateById(riderWallet);
            }
        }



    }
}
